//
//  DrawViewController.m
//  Runner
//
//  Created by 小二同學 on 2021/6/28.
//

#import "DrawViewController.h"
#import "LSDrawView.h"

@interface DrawViewController ()
{
    LSDrawView *drawView;
    UIImageView *bgImgView;
}
@property (strong,nonatomic) AFHTTPSessionManager *mgr;
@end

@implementation DrawViewController

- (void)setImagePath:(NSString *)imagePath {
    _imagePath = imagePath;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_STATUSBAR_NAVBAR_HEIGHT)];
    navView.backgroundColor = UIColorFromRGB(0x1296db);
    [self.view addSubview:navView];
    
    UIButton *leftItem = [[UIButton alloc] initWithFrame:CGRectMake(20, GK_STATUSBAR_HEIGHT + 10, 25, 30)];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"avcBackIcon"] forState:UIControlStateNormal];
    [leftItem addTarget:self action:@selector(onClickClose:) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftItem];
    
    UIButton *rightItem = [[UIButton alloc] initWithFrame:CGRectMake(GK_SCREEN_WIDTH - 60, GK_STATUSBAR_HEIGHT + 10, 40, 30)];
    [rightItem setTitle:@"保存" forState:UIControlStateNormal];
    [rightItem addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightItem];
    
    self.view.backgroundColor = [UIColor whiteColor];    
    
    drawView = [[LSDrawView alloc] initWithFrame:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT - GK_STATUSBAR_NAVBAR_HEIGHT)];
    drawView.brushColor = [UIColor redColor];
    drawView.brushWidth = 3;
    drawView.shapeType = LSShapeCurve;
    
    drawView.backgroundImage = [UIImage imageWithContentsOfFile:self.imagePath];
    
    [self.view addSubview:drawView];
    
    UIButton *btnUndo = [UIButton buttonWithType:UIButtonTypeCustom];
    btnUndo.backgroundColor = [UIColor orangeColor];
    btnUndo.frame = CGRectMake(20, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 70, 60, 20);
    [btnUndo setTitle:@"撤销" forState:UIControlStateNormal];
    [btnUndo addTarget:self action:@selector(btnUndoClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnUndo];
    
    
    UIButton *btnRedo = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRedo.backgroundColor = [UIColor orangeColor];
    btnRedo.frame = CGRectMake(100, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 70, 60, 20);
    [btnRedo setTitle:@"重做" forState:UIControlStateNormal];
    
    [btnRedo addTarget:self action:@selector(btnRedoClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnRedo];
    
    UIButton *btnClean = [UIButton buttonWithType:UIButtonTypeCustom];
    btnClean.backgroundColor = [UIColor orangeColor];
    btnClean.frame = CGRectMake(180, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 70, 60, 20);
    [btnClean setTitle:@"清除" forState:UIControlStateNormal];
    
    [btnClean addTarget:self action:@selector(btnCleanClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnClean];
    
    
    
    UIButton *btnCurve = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCurve.backgroundColor = [UIColor orangeColor];
    btnCurve.frame = CGRectMake(260, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 70, 60, 20);
    [btnCurve setTitle:@"曲线" forState:UIControlStateNormal];
    
    [btnCurve addTarget:self action:@selector(btnCurveClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnCurve];
    
    
    UIButton *btnLine = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLine.backgroundColor = [UIColor orangeColor];
    btnLine.frame = CGRectMake(20, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 40, 60, 20);
    [btnLine setTitle:@"直线" forState:UIControlStateNormal];
    
    [btnLine addTarget:self action:@selector(btnLineClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnLine];
    
    
    UIButton *btnEllipse = [UIButton buttonWithType:UIButtonTypeCustom];
    btnEllipse.backgroundColor = [UIColor orangeColor];
    btnEllipse.frame = CGRectMake(100, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 40, 60, 20);
    [btnEllipse setTitle:@"椭圆" forState:UIControlStateNormal];
    
    [btnEllipse addTarget:self action:@selector(btnEllipseClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnEllipse];
    
    
    UIButton *btnRect = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRect.backgroundColor = [UIColor orangeColor];
    btnRect.frame = CGRectMake(180, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 40, 60, 20);
    [btnRect setTitle:@"矩形" forState:UIControlStateNormal];
    
    [btnRect addTarget:self action:@selector(btnRectClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnRect];
    
    UIButton *btnEraser = [UIButton buttonWithType:UIButtonTypeCustom];
    btnEraser.backgroundColor = [UIColor orangeColor];
    btnEraser.frame = CGRectMake(260, GK_SCREEN_HEIGHT - GK_TABBAR_HEIGHT - 40, 60, 20);
    [btnEraser setTitle:@"橡皮擦" forState:UIControlStateNormal];
    [btnEraser setTitle:@"画笔" forState:UIControlStateSelected];
    
    [btnEraser addTarget:self action:@selector(btnEraserClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnEraser];
}

- (void)onClickClose:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 确定
- (void)save:(id)sender {
    [drawView doneWithImagePathSuccess:^(NSString *imagePath) {

        AFHTTPSessionManager *manager = [self getManager];

        NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名，以免重复，在测试的时候其实可以判断文件是否存在若存在，则删除，重新生成文件即可
        [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
        NSString *drawImagePath = [NSString stringWithFormat:@"drawImage_%@.jpg", [formater stringFromDate:[NSDate date]]];

        [manager POST:[NSString stringWithFormat:@"%@basics/v1/file",Server_URL] parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileURL:[NSURL fileURLWithPath:imagePath] name:@"file" fileName:drawImagePath mimeType:@"jpg" error:nil];
        } progress:^(NSProgress * _Nonnull uploadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            NSDictionary *dic1 = (NSDictionary *)responseObject;

            if ([dic1[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray *arr = dic1[@"data"];
                if([self.delegate respondsToSelector:@selector(DrawImageUploadDelegate:)]){
                    [self.delegate DrawImageUploadDelegate:arr[0]];
                }
            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnRecClicked:(id)sender
{
    [drawView testRecToFile];
}

- (void)btnPlayClicked:(id)sender
{
    [drawView testPlayFromFile];
}


- (void)btnSaveClicked:(id)sender
{
    [drawView save];
}

- (void)btnCleanClicked:(id)sender
{
    [drawView clean];
}


- (void)btnRectClicked:(id)sender
{
    drawView.shapeType = LSShapeRect;
}

- (void)btnEllipseClicked:(id)sender
{
    drawView.shapeType = LSShapeEllipse;
}

- (void)btnLineClicked:(id)sender
{
    drawView.shapeType = LSShapeLine;
}

- (void)btnCurveClicked:(id)sender
{
    drawView.shapeType = LSShapeCurve;
}

- (void)btnEraserClicked:(id)sender
{
    UIButton *btn = (UIButton*)sender;
    if (btn.selected)
    {
        btn.selected = NO;
        
        //使用画笔
        drawView.isEraser = NO;
    }
    else
    {
        btn.selected = YES;
        
        //使用橡皮擦
        drawView.isEraser = YES;
    }
}

- (void)btnUndoClicked:(id)sender
{
    [drawView unDo];
}

- (void)btnRedoClicked:(id)sender
{
    [drawView reDo];
}

- (AFHTTPSessionManager *)getManager
{
    _mgr = [AFHTTPSessionManager manager];
    [_mgr.requestSerializer setTimeoutInterval:10];
    _mgr.responseSerializer = [AFJSONResponseSerializer serializer];
    _mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
    _mgr.requestSerializer = [AFJSONRequestSerializer serializer];
    return _mgr;
}
@end
